package Others;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class _797_AllPathsFromSourcetoTarget {
    List<List<Integer>> ans;
    int length;
    int[][] graph;
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        this.length = graph.length-1;
        this.graph = graph;
        this.ans = new ArrayList<>();
        for (int i : graph[0]) {
            List<Integer> tList = new ArrayList<>();
            tList.add(0);
            tList.add(i);
            addNode(tList, i);
        }
        return ans;
    }

    public void addNode(List<Integer> tempList, int val) {
        if (val == length) {
            ans.add(tempList);
        } else {
            for (int i : graph[val]) {
                List tList = new ArrayList<>(tempList);
                tList.add(i);
                addNode(tList, i);
            }
        }
    }
}
